package com.thinvent.smart.chain.common;

import com.alipay.mychain.sdk.api.env.ClientEnv;
import com.alipay.mychain.sdk.api.env.ISslOption;
import com.alipay.mychain.sdk.api.env.SignerOption;
import com.alipay.mychain.sdk.api.env.SslBytesOption;
import com.alipay.mychain.sdk.api.logging.AbstractLoggerFactory;
import com.alipay.mychain.sdk.api.logging.ILogger;
import com.alipay.mychain.sdk.api.utils.Utils;
import com.alipay.mychain.sdk.crypto.MyCrypto;
import com.alipay.mychain.sdk.crypto.keyoperator.Pkcs8KeyOperator;
import com.alipay.mychain.sdk.crypto.keypair.Keypair;
import com.alipay.mychain.sdk.crypto.signer.SignerBase;
import com.alipay.mychain.sdk.domain.account.Identity;
import com.alipay.mychain.sdk.utils.IOUtil;
import com.thinvent.smart.chain.contract.AntChainDemo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

public class ChainEnv {
    public ClientEnv build;
    private Keypair userKeypair;
    private Identity userIdentity;

    public void initMychainEnv(String account,String userPassword){
//        String account = "yujwTest";
//        String userPassword = "123456";
        // any user key for sign message
        String userPrivateKeyFile = "user.key";
        userIdentity = Utils.getIdentityByName(account); // 根据实际情况更新'gushui03'为'user.key'对应的账户名(BaaS申请证书时创建的账户名)
        Pkcs8KeyOperator pkcs8KeyOperator = new Pkcs8KeyOperator();
        try {
            userKeypair = pkcs8KeyOperator.load(
                    IOUtil.inputStreamToByte(ChainEnv.class.getClassLoader().getResourceAsStream(userPrivateKeyFile)),
                    userPassword);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        build = buildMychainEnv();

        ILogger logger = AbstractLoggerFactory.getInstance(AntChainDemo.class);
        build.setLogger(logger);
    }

    public ClientEnv buildMychainEnv() {

        String host1 = "47.111.195.13"; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的IP地址。
        int port1 = 18133; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的端口号。
        String host2 = "47.111.195.13"; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的IP地址。
        int port2 = 18131; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的端口号。
        String host3 = "47.111.195.13"; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的IP地址。
        int port3 = 18132; //根据实际情况更新，在BaaS平台，通过查看目标合约链"详情"，在"浏览器"中查看"节点详情"可获取链节点的端口号。
        String keyPassword = "123456";  //TODO 申请证书时候指定的SSL密码
        String trustStorePassword = "mychain";



        String keyFilePath = "client.key";
        String certFilePath = "client.crt";
        String trustStoreFilePath = "trustCa";
        // build ssl option
        ISslOption sslOption = null;
        try {
            sslOption = new SslBytesOption.Builder()
                    .keyBytes(IOUtil.inputStreamToByte(ChainEnv.class.getClassLoader().getResourceAsStream(keyFilePath)))
                    .certBytes(IOUtil.inputStreamToByte(ChainEnv.class.getClassLoader().getResourceAsStream(certFilePath)))
                    .keyPassword(keyPassword)
                    .trustStorePassword(trustStorePassword)
                    .trustStoreBytes(
                            IOUtil.inputStreamToByte(ChainEnv.class.getClassLoader().getResourceAsStream(trustStoreFilePath)))
                    .build();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<InetSocketAddress> socketAddressArrayList = new ArrayList<InetSocketAddress>();
        InetSocketAddress inetSocketAddress1 = InetSocketAddress.createUnresolved(host1, port1);
        InetSocketAddress inetSocketAddress2 = InetSocketAddress.createUnresolved(host2, port2);
        InetSocketAddress inetSocketAddress3 = InetSocketAddress.createUnresolved(host3, port3);
        socketAddressArrayList.add(inetSocketAddress1);
        socketAddressArrayList.add(inetSocketAddress2);
        socketAddressArrayList.add(inetSocketAddress3);
        List<SignerBase> signerBaseList = new ArrayList<SignerBase>();
        SignerBase signerBase = MyCrypto.getInstance().createSigner(userKeypair);
        signerBaseList.add(signerBase);
        SignerOption signerOption = new SignerOption();
        signerOption.setSigners(signerBaseList);
        return ClientEnv.build(socketAddressArrayList, sslOption, signerOption);
    }

    public ClientEnv getBuild() {
        return build;
    }

    public void setBuild(ClientEnv build) {
        this.build = build;
    }

    public Keypair getUserKeypair() {
        return userKeypair;
    }

    public void setUserKeypair(Keypair userKeypair) {
        this.userKeypair = userKeypair;
    }

    public Identity getUserIdentity() {
        return userIdentity;
    }

    public void setUserIdentity(Identity userIdentity) {
        this.userIdentity = userIdentity;
    }
}
